import sys
from gurobipy import *
import ALGhelper
def MainFunction(filename):
    data = ALGhelper.ReadFile(filename)

    F = range(data.fnum)
    C = range(data.cnum)

    #Model
    m = Model("facility")

    y = []
    for f in F:
        y.append(m.addVar(name = "Fac%d"%f))

    x = []
    for f in F:
        x.append([])
        for c in C:
            x[f].append(m.addVar(name = "Cli%d_Fac%d"%(c,f)))

    z = []
    for c in C:
        z.append(m.addVar(name = "Penalty%d"%c))

    m.modelSense = GRB.MINIMIZE

    m.update()

    m.setObjective(quicksum([data.f[i]*y[i] for i in F])+quicksum([quicksum([data.c[i][j] * x[i][j] for j in C]) for i in F])+ quicksum([data.p[j] * z[j] for j in C]))

    for i in F:
        m.addConstr(y[i] >= 0)

    for i in F:
        for j in C:
            m.addConstr(x[i][j] >= 0)

    for j in C:
        m.addConstr(z[j] >= 0)


    for j in C:
        m.addConstr(quicksum([x[i][j] for i in F]) + z[j] >= 1)

    for i in F:
        m.addConstr(data.u[i]*y[i] >= quicksum([x[i][j] for j in C]))

    m.optimize()

    return m.objVal


